var arr = [];//存所有数据
var count = 8;//一页多少条数据
var page = 1;//当前的页数
var n;
var user = JSON.parse(localStorage.getItem('user'));
var arr2 = [];
arr2.push(user);
console.log(user.type);
console.log(user.name);
// 插入

function rem() {
    $('.motaikuang table tbody').html('');
    if (user.type == 2) {
        $.each(arr.slice((page - 1) * count, page * count), function (i, v) {
            var a = v.type == '1' ? '普通管理员' : "超级管理员";
            $('.motaikuang table tbody').append('<tr data-pass="' + v.password + '">\
    <td class="th1">'+ v.id + '</td>\
    <td>'+ v.name + '</td>\
    <td>'+ v.userId + '</td>\
    <td>'+ a + '</td>\
    <td>\
            <button type="button" class="shu" data-toggle="modal" data-target="#myModal5">\
                删除\
            </button>\
            <button type="button" class="shu"\ data-toggle="modal"data-target="#exampleModal2"\
                data-whatever="@mdo">修改</button>\
    </td>\
</tr>'
            )
        })
    } else if (user.type == 1) {
        $.each(arr.slice((page - 1) * count, page * count), function (i, v) {
            var a = v.type == '1' ? '普通管理员' : "超级管理员";
            $('.motaikuang table tbody').append('<tr data-pass="' + v.password + '">\
    <td class="th1">'+ v.id + '</td>\
    <td>'+ v.name + '</td>\
    <td>'+ v.userId + '</td>\
    <td>'+ a + '</td>\
    <td>\
            <button type="button" class="shu" data-toggle="modal" data-target="#myModal5">\
                删除\
            </button>\
            <button type="button" class="shu"\ data-toggle="modal"data-target="#exampleModal2"\
                data-whatever="@mdo">修改</button>\
    </td>\
</tr>'
            )
        })
    }
    function rwt() {
        $.ajax({
            url: 'http://172.17.7.212:3000/admin/getadmin',
            success: function (res) {
                console.log(res)
                if (res.data.length) {
                    arr = res.data;
                    rem();
                    ret();
                }

            },
        })
    }
    rwt();
    // 页码的点击
    $('#nav2').on('click', 'li', function () {
        page = $(this).text();
        rem();
    })
    function ret() {
      
    }
    // 添加
    $('.j-add-btn').on('click', function () {
        if (user.type == 2) {
            // console.log(11);
            $.ajax({
                url: 'http://172.17.7.212:3000/admin/addadmin',
                data: {
                    name: $('.j-add-name').val(),
                    password: $('.j-add-password').val(),
                    type: $('.j-add-type').val(),
                    userId: $('.j-add-user').val(),
                },

                type: 'post',
                success: function (res) {
                    // console.log(res)
                    if (res.code == 200) {
                        $('#exampleModal1').modal('hide');
                        rwt();
                    } else {
                        alert('添加失败')
                    }
                }
            })
        } else if (user.type == 1) {
            window.location.href = '/wb258-2/page/AnnouncementHomePage2.html'
        }
    })
    //删除
    // 当点击确定删除按钮的时候
    if (user.type == 2) {
        $('.guanlishanchu').on('click', function () {
            $.ajax({
                url: '/api/admin/deladmin',
                data: {
                    id: n
                },
                type: 'post',
                success: function (res) {
                    // console.log(res)
                    if (res.code == 200) {
                        $('#myModal5').modal('hide');
                        rwt();
                    } else {
                        alert('删除失败')
                    }
                }
            })
        })
    } else if (user.type == 1) {
        window.location.type = '/wb258-2/page/AnnouncementHomePage2.html'
    }

    // 点击删除的时候
    $('#myModal5').on('show.bs.modal', function (e) {
        n = $(e.relatedTarget).parents('tr').children().eq(0).text();

    })
    //修改
    // 当点击修改的时候
    if (user.type == 2) {
        $('#exampleModal2').on('show.bs.modal', function (e) {
            var tr = $(e.relatedTarget).parents('tr');
            $('.j-s-id').val(tr.children().eq(0).text());
            $('.j-s-name').val(tr.children().eq(1).text());

            $('.j-s-wp').val(tr.children().eq(2).text());

            $('.j-s-pass').val(tr.attr('data-pass'));

            $('.j-s-type option[value=' + tr.children().eq(3).text() + ']').attr('selected', 'selected');


        })
    } else if (user.type == 1) {
        window.location.type = '/wb258-2/page/AnnouncementHomePage2.html'
    }

    // 当点击确定的时候
    if (user.type == 2) {
        $('.j-s-q').on('click', function () {

            $.ajax({
                url: '/api/admin/updateadmin',
                data: {
                    id: $('.j-s-id').val(),
                    name: $('.j-s-name').val(),
                    userId: $('.j-s-wp').val(),
                    password: $('.j-s-pass').val(),
                    type: $('.j-s-type').val() == "普通管理员" ? 1 : 2
                },
                type: 'post',
                success: function (res) {
                    if (res.code == 200) {
                        $('#exampleModal2').modal('hide');
                        rwt();
                    } else {
                        alert("修改失败")
                    }
                }
            })
        })
    } else if (user.type == 1) {
        window.location.type = '/wb258-2/page/AnnouncementHomePage2.html'
    }



